import { IErrorInfo } from '@/types/common'
import { apiPost, deleteUserToken } from '@/utils'

// 用户信息
export interface IUserInfo {
  user_id: number // 用户ID
  username: string // 用户名
  phone: string // 手机号
  nickname: string // 昵称
  avatar: string | null // 头像
  register_at: string // 注册时间
}

// 获取首页数据
export const getHomeData = async (): Promise<{ success: boolean; data: string | IErrorInfo }> => {
  try {
    const res = await apiPost<string>('/home')
    return { success: true, data: res }
  } catch (e) {
    return { success: false, data: e }
  }
}

// 获取用户信息
export const getUserInfo = async (): Promise<{ success: boolean; data: IUserInfo | IErrorInfo }> => {
  try {
    const res = await apiPost<IUserInfo>('/user/info')
    return { success: true, data: res }
  } catch (e) {
    return { success: false, data: e }
  }
}

// 退出登录
export const logoutOut = async (): Promise<{ success: boolean; data?: IErrorInfo }> => {
  try {
    await apiPost<IUserInfo>('/user/logout')
    deleteUserToken()
    return { success: true }
  } catch (e) {
    return { success: false, data: e }
  }
}

// 使token过期
export const expiredToken = async (): Promise<{ success: boolean; data?: IErrorInfo }> => {
  try {
    await apiPost<IUserInfo>('/user/expiredToken')
    return { success: true }
  } catch (e) {
    return { success: false, data: e }
  }
}
